import psutil


def disk_schema(path):
    disk = psutil.disk_usage(path)
    result = {
        'name': path,
        'total': '%.2f GB' % (disk.total / 1024 / 1024 / 1024),
        'used': '%.2f GB' % (disk.used / 1024 / 1024 / 1024),
        'percent': '{}%'.format(disk.percent)
    }
    return result


def system_info():
    cpu_count = psutil.cpu_count()
    cpu_percent = psutil.cpu_percent()
    memory = psutil.virtual_memory()
    disk = psutil.disk_partitions()
    data = {
        'CPU': {
            'count': cpu_count,
            'percent': cpu_percent
        },
        'Memory': {
            'total': '%.2f GB' % (memory.total/1024/1024/1024),
            'used': '%.2f GB' % (memory.used/1024/1024/1024),
            'percent': memory.percent
        },
        'Disk': {
            'count': len(disk),
            'data': [disk_schema(path.mountpoint) for path in disk]
        }
    }
    return data


def software_schema(software):
    return {
        'id': software.id,
        'name': software.name,
        'status': software.status,
        'version': software.version,
        'path': software.path,
        'slug': software.slug,
        'describe': software.describe
    }
